package com.kt.uokos.yimo.service.impl;

import com.kt.uokos.yimo.dao.Cart;
import com.kt.uokos.yimo.example.CartExample;
import com.kt.uokos.yimo.mapper.CartMapper;
import com.kt.uokos.yimo.service.CartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @description: TODO
 * @className: CartServiceImpl.java
 * @author: Kang Hao
 * @date: 2022/4/23 20:26
 **/

@Service
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;

    @Override
    public Integer countByUid(Integer uId) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(uId).andDeletedEqualTo(false);
        return (int)cartMapper.countByExample(example);
    }

    @Override
    public List<Cart> queryByUid(Integer uId) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(uId).andDeletedEqualTo(false);
        return cartMapper.selectByExample(example);
    }

    @Override
    public void deletedById(Integer id) {
        cartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateChecked(Integer userId, List<Integer> productIds, Boolean isChecked) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andProductIdIn(productIds).andDeletedEqualTo(false);
        Cart cart = new Cart();
        cart.setChecked(isChecked);
        cart.setUpdateTime(LocalDateTime.now());
        return cartMapper.updateByExampleSelective(cart,example);
    }

    @Override
    public void delete(Integer userId, List<Integer> productIds) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andProductIdIn(productIds);
        cartMapper.deleteByExample(example);
    }

    @Override
    public Cart queryExist(Integer userId, Integer goodsId, Integer goodsProductId) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andGoodsIdEqualTo(goodsId).andProductIdEqualTo(goodsProductId);
        return cartMapper.selectOneByExample(example);
    }

    @Override
    public void add(Cart cart) {
        cart.setAddTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        cart.setDeleted(false);
        cartMapper.insertSelective(cart);
    }
}
